package com.devinlee.designpatterns.observer;

import com.devinlee.designpatterns.observer.consumer.MessageListent;
import com.devinlee.designpatterns.observer.mediator.MessageManagement;

/**
 * 观察者模式-商家店铺
 * <p>
 * Created by devinlee in 2022.07.04
 */
public class Shop {
    // 商品
    private Goods goods;
    // 消息管理
    private MessageManagement messageManagement;

    /**
     * 初始化活动类型
     */
    public Shop() {
        this("618", "双11");
    }

    /**
     * 初始化活动类型
     * @param messageTypes
     */
    public Shop(String... messageTypes) {
        this.messageManagement = new MessageManagement(messageTypes);
    }

    public void addGoods(String name, double price){
        this.goods = new Goods(name, price);
    }

    /**
     * 开启618活动
     */
    public void start618(){
        if (this.goods == null){
            throw new RuntimeException("Please add goods first!");
        }
        this.messageManagement.call("618", goods);
    }

    /**
     * 开启11活动
     */
    public void start11(){
        if (this.goods == null){
            throw new RuntimeException("Please add goods first!");
        }
        this.messageManagement.call("双11", goods);
    }

    /**
     * 订阅
     * @param messageType
     * @param messageListent
     */
    public void subscribe(String messageType, MessageListent messageListent){
        this.messageManagement.subscribe(messageType, messageListent);
    }

    /**
     * 退订
     * @param messageType
     * @param messageListent
     */
    public void unsubscribe(String messageType, MessageListent messageListent){
        this.messageManagement.unsubscribe(messageType, messageListent);
    }
}
